﻿using SharePointContentGenerator.ContentGeneratorLogic.Controllers;
using System.Diagnostics.Contracts;
using System.Collections.Generic;

namespace SharePointContentGenerator.ContentGeneratorLogic
{
    public class ContentGeneratorFacade
    {
        #region Word Document Management

        public void CreateWordDocument(string documentPath)
        {
            WordDocumentController controller = new WordDocumentController();
            controller.CreateWordDocument(documentPath);
        }

        #endregion

        #region SharePoint Document Library Management

        /// <summary>
        /// This methods creates a number of lists in a given site collection.
        /// When no parameters are passed for authentication, the logged in user credentials are used.
        /// This method can also create documents within the document library.
        /// </summary>
        /// <param name="siteCollection">Url of the site collection, i.e. http://server/site, cannot be null or empty</param>
        /// <param name="baseLibraryName">The base name of the library to create, cannot be null or empty</param>
        /// <param name="numberOfLibraries">Number of libraries to create, must be strictly greater than 0</param>
        /// <param name="deleteIfExist">If true, it will delete the list if it already exist - optional parameter, defaults to false</param>
        /// <param name="addRandomDocuments">If true, it will add random Word documents in the list - optional parameter, defaults to false</param>
        /// <param name="minRandomDocument">Minimum random of documents to add - optional parameter, defaults to 10</param>
        /// <param name="maxRandomdocument">Maximum random of documents to add - optional parameter, defaults to 50</param>
        /// <param name="domain">Domain used for authenticaiton - optional parameter</param>
        /// <param name="username">Username used for authentication - optional parameter</param>
        /// <param name="password">Password used for authentication - optional parameter</param>
        /// <returns>A list of the names of the newly created document libraries</returns>
        public List<string> CreateDocumentLibraries(string siteCollection, string baseLibraryName = "", int numberOfLibraries = 1, bool deleteIfExist = false, bool addRandomDocuments = false, int minRandomDocument = 2, int maxRandomdocument = 5, string domain = "", string username = "", string password = "")
        {
            Contract.Requires(siteCollection != null, "The site collection cannot be null");
            Contract.Requires(siteCollection != string.Empty, "The site collection cannot be string.empty");
            Contract.Requires(domain != null, "The domain cannot be null");
            Contract.Requires(username != null, "The username cannot be null");
            Contract.Requires(password != null, "The password cannot be null");
            Contract.Requires(baseLibraryName != null, "The base library name cannot be null");
            Contract.Requires(numberOfLibraries > 0, "The number of libraries cannot be less than 1");
            Contract.Requires(minRandomDocument > 0, "The minimum number of document must be strictly positive");
            Contract.Requires(maxRandomdocument > 0, "The maximum number of document must be strictly positive");
            Contract.Requires(maxRandomdocument > minRandomDocument, "The maximum number of document must be greater than the minimum number of documents");

            SharePointDocumentLibraryController controller = new SharePointDocumentLibraryController(siteCollection, domain, username, password);
            List<string> listNameList = controller.CreateDocumentLibraries(baseLibraryName, numberOfLibraries, deleteIfExist);
            if (addRandomDocuments)
                controller.PopulateLibrariesWithDocuments(listNameList, minRandomDocument, maxRandomdocument);

            return listNameList;
        }

        /// <summary>
        /// This methods creates a number of documents in the document libraries passed as parameter.
        /// When no parameters are passed for authentication, the logged in user credentials are used.
        /// </summary>
        /// <param name="siteCollection">Url of the site collection, i.e. http://server/site, cannot be null or empty</param>
        /// <param name="listNameList">A list of the names of the document libraries within which to create the documents</param>
        /// <param name="minRandomDocument">Minimum random of documents to add - optional parameter, defaults to 10</param>
        /// <param name="maxRandomdocument">Maximum random of documents to add - optional parameter, defaults to 50</param>
        /// <param name="domain">Domain used for authenticaiton - optional parameter</param>
        /// <param name="username">Username used for authentication - optional parameter</param>
        /// <param name="password">Password used for authentication - optional parameter</param>
        public void CreateDocumentsInDocumentLibraries(string siteCollection, List<string> listNameList, int minRandomDocument = 2, int maxRandomdocument = 5, string domain = "", string username = "", string password = "")
        {
            Contract.Requires(siteCollection != null, "The site collection cannot be null");
            Contract.Requires(siteCollection != string.Empty, "The site collection cannot be string.empty");
            Contract.Requires(domain != null, "The domain cannot be null");
            Contract.Requires(username != null, "The username cannot be null");
            Contract.Requires(password != null, "The password cannot be null");
            Contract.Requires(minRandomDocument > 0, "The minimum number of document must be strictly positive");
            Contract.Requires(maxRandomdocument > 0, "The maximum number of document must be strictly positive");
            Contract.Requires(maxRandomdocument > minRandomDocument, "The maximum number of document must be greater than the minimum number of documents");

            SharePointDocumentLibraryController controller = new SharePointDocumentLibraryController(siteCollection, domain, username, password);
            controller.PopulateLibrariesWithDocuments(listNameList, minRandomDocument, maxRandomdocument);
        }

        #endregion
    }
}
